﻿Shader "Nave/ShaderFX-Shield" 
{
    Properties
    {
        _MainColor("MainColor", Color) = (1,1,1,1)
        _MainTex("Texture", 2D) = "white" {}
        _Fresnel("Fresnel Intensity", Range(0,200)) = 3.0
        _FresnelWidth("Fresnel Width", Range(0,2)) = 3.0
        _Distort("Distort", Range(0, 100)) = 1.0
        _IntersectionThreshold("Intersection threshold", range(0,1)) = 0.1 
        _ScrollSpeedU("Scroll U Speed",float) = 2
        _ScrollSpeedV("Scroll V Speed",float) = 0
    }

    SubShader
    {
        Tags{ "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

        //为扭曲背景需要抓帧。 实际项目需要单独优化
        GrabPass{ "_GrabTexture" }

        Pass
        {
            Lighting Off ZWrite On
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Off

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                fixed4 vertex : POSITION;
                fixed4 normal : NORMAL;
                fixed3 uv : TEXCOORD0;
            };

            struct v2f
            {
                fixed2 uv : TEXCOORD0;
                fixed4 vertex : SV_POSITION;
                fixed3 rimColor : TEXCOORD1;
                fixed4 screenPos : TEXCOORD2;
            };

            sampler2D _MainTex, _CameraDepthTexture, _GrabTexture;
            fixed4 _MainTex_ST,_MainColor,_GrabTexture_ST, _GrabTexture_TexelSize;
            fixed _Fresnel, _FresnelWidth, _Distort, _IntersectionThreshold, _ScrollSpeedU, _ScrollSpeedV;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                //滚动UV
                o.uv.x += _Time * _ScrollSpeedU;
                o.uv.y += _Time * _ScrollSpeedV;

                //视角方向 计算菲涅尔效果 
                fixed3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                fixed oneMinusDotNV = 1 - saturate(dot(v.normal, viewDir));
                o.rimColor = smoothstep(1 - _FresnelWidth, 1.0, oneMinusDotNV) * 0.5;

                o.screenPos = ComputeScreenPos(o.vertex);
                COMPUTE_EYEDEPTH(o.screenPos.z);
                return o;
            }

            fixed4 frag(v2f i, fixed face : VFACE) : SV_Target
            {
                float eyeLinearDepth = LinearEyeDepth(tex2Dproj(_CameraDepthTexture,i.screenPos).r);
                fixed intersect = saturate((abs(eyeLinearDepth - i.screenPos.z)) / _IntersectionThreshold);
                
                fixed3 color = tex2D(_MainTex, i.uv);

                //扭曲背景
                i.screenPos.xy += (color.rg * 2 - 1) * _Distort * _GrabTexture_TexelSize.xy;
                fixed3 distortColor = tex2Dproj(_GrabTexture, i.screenPos);
                distortColor *= _MainColor * _MainColor.a + 1;

                //相交区域高光
                i.rimColor *= intersect * clamp(0,1,face);
                color *= _MainColor * pow(_Fresnel,i.rimColor);
                
                color = lerp(distortColor, color, i.rimColor.r);
                color += (1 - intersect) * (face > 0 ? 0.03 : 0.3) * _MainColor * _Fresnel;
                return fixed4(color,0.4);
            }
            ENDCG
        }
    }
}